package com.mathdroid.model.enums;

import java.util.LinkedList;

import com.mathdroid.exceptionhandling.ParserException;

/**
 * Each item has a source or drop location.
 */
public enum EItemSource {
	
	/**
	 * Player versus Player.
	 */
	PVP("PvP", "pvp"),
	/**
	 * Crafting.
	 */
	CRAFTING("Crafting", "crafting"),
	/**
	 * Operation: Eternity Vault.
	 */
	ETERNITY_VAULT("Eternity Vault", "eternityvault"),
	/**
	 * Unknown source.
	 */
	UNKNOWN("Unknown", "unknown");
	
	/**
	 * The description of the source.
	 */
	private String name = null;
	
	/**
	 * The icon name.
	 */
	private String icon = null;
	
	/**
	 * Create a source enumeration.
	 * @param n Name.
	 * @param i Icon name.
	 */
	EItemSource(final String n, final String i) {
		this.name = n;
		this.icon = i;
	}
	
	@Override
	public final String toString() {
		return this.name;
	}
	
	/**
	 * Get all item sources.
	 * @return Item sources.
	 */
	public static final LinkedList<EItemSource> getAllSources() {
		LinkedList<EItemSource> sources = new LinkedList<EItemSource>();
		
		sources.add(CRAFTING);
		sources.add(ETERNITY_VAULT);
		sources.add(PVP);
		sources.add(UNKNOWN);
		
		return sources;
	}

	/**
	 * Get the name of the icon.
	 * @return The icon name.
	 */
	public String getIcon() {
		return this.icon;
	}
	
	/**
	 * Parse a string to an enumeration object. The parser compares the
	 * string representation of this enumeration with the parameter, not the
	 * literal.
	 * @param t String.
	 * @return EItemSource object.
	 * @throws ParserException The string cannot be parsed.
	 */
	public static final EItemSource parseEItemSource(final String t) 
	throws ParserException {
		EItemSource s = null;
		
		for (EItemSource src : EItemSource.getAllSources()) {
			if (t.equals(src.toString())) {
				s = src;
			}
		}
		
		if (s == null) {
			ParserException p = new ParserException("No such source: \"" 
					+ s + "\"");
			p.fillInStackTrace();
			throw p;
		}
		
		return s;
	}
}
